package com.swing_pinea.core;

import com.swing_pinea.event.PineaEvent;
import com.swing_pinea.render.GameWindow;
import com.swing_pinea.render.PineaColor;
import com.swing_pinea.render.PineaImage;
import com.swing_pinea.shape.PineaRect;

import javax.imageio.ImageIO;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class Pinea {


    public static final int PINEA_EVENT_KEY_UP = 0;
    public static final int PINEA_EVENT_KEY_DOWN = 1;
    public static final int PINEA_EVENT_QUIT = 6;

    public static final int PINEA_KEY_A = KeyEvent.VK_A;
    public static final int PINEA_KEY_D = KeyEvent.VK_D;
    public static final int PINEA_KEY_W = KeyEvent.VK_W;
    public static final int PINEA_KEY_S = KeyEvent.VK_S;
    public static final int PINEA_KEY_SPACE = KeyEvent.VK_SPACE;

    public static void pineaInit(String title, int width, int height) {
        if (GameWindow.getInstance() == null) {
            new GameWindow(title, width, height);
        }
    }

    public static void pineaQuit() {
        GameWindow.getInstance().quit();
    }

    public static void pineaShow() {
        GameWindow.getInstance().show();
    }

    public static boolean pineaPollEvent(PineaEvent event) {
        if (GameWindow.getInstance().getEventQueue().isEmpty()) {
            return false;
        }
        PineaEvent e = GameWindow.getInstance().getEventQueue().poll();
        event.type = e.type;
        event.key = e.key;
        return true;
    }

    public static void pineaClear(PineaColor color) {
        GameWindow.getInstance().getGamePanel().clear(color);
    }

    public static void pineaRender() {
        GameWindow.getInstance().getGamePanel().render();
    }

    public static void pineaFillRect(PineaRect rect, PineaColor color) {
        GameWindow.getInstance().getGamePanel().fillRect(rect, color);
    }

    public static PineaImage pineaLoadImage(String path) {
        try {
            BufferedImage data = ImageIO.read(new File(path));
            return new PineaImage(data);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static void pineaDrawImage(PineaImage image, float x, float y) {
        GameWindow.getInstance().getGamePanel().drawImage(image, x ,y);
    }
    
    public static void pineaDrawImageStatic(PineaImage image, float x, float y) {
        GameWindow.getInstance().getBgPanel().drawImage(image, x ,y);
    }

}
